package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.Goods;
import com.cskaoyan.bean.pojo.GoodsExample;
import com.cskaoyan.bean.pojo.Topic;
import com.cskaoyan.bean.pojo.TopicExample;
import com.cskaoyan.bean.vo.wx.TopicVO;
import com.cskaoyan.beanwx.bo.WxBaseParam;
import com.cskaoyan.beanwx.vo.TopicDetailVO;
import com.cskaoyan.beanwx.vo.TopicGoodsVO;
import com.cskaoyan.beanwx.vo.TopicListVO;
import com.cskaoyan.dao.GoodsMapper;
import com.cskaoyan.dao.TopicMapper;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @description: TopocService实现类
 * @Author: 刘森飚
 */
@Service
public class TopicServiceImp implements TopicService {
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public TopicListVO queryTopicListByPageAndSize(WxBaseParam param) {
        PageHelper.startPage(param.getPage(),param.getSize());

        TopicExample example = new TopicExample();
        TopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Topic> mallTopics = topicMapper.selectByExample(example);
        long count = topicMapper.countByExample(example);
        TopicListVO listVO = new TopicListVO(mallTopics, (int) count);

        return listVO;
    }

    @Override
    public TopicDetailVO getTopicDetail(Integer id) {
        TopicDetailVO topicDetail = new TopicDetailVO();
        //topic + list<goods>
        Topic topic = getTopicById(id);
        List<TopicGoodsVO> goods = getGoodsListByIdArray(topic.getGoods());
        topicDetail.setTopic(topic);
        topicDetail.setGoods(goods);
        return topicDetail;
    }

    @Override
    public List<Topic> getRelatedTopics(Integer id) {
        //TODO : related?

        List<Topic> topics = new ArrayList<>();
        int count = 0;
        int sign = 1;
        for (int i = 0; i < 100; i++, count++) {
            if (topics.size() >= 4) {
                break;
            }
            int newId = id + sign * count;
            if (newId <= 0) {
                break;
            }
            Topic newTopic = getTopicById(newId);
            if (newTopic != null) {
                topics.add(getTopicById(newId));
            }
        }
        return topics;
    }

    /**
     * @description: 通过商品id的Integer数组获得商品List
     * @param: Integet[]
     * @return: List
     * @Author: liusenbiao
     */
    private List<TopicGoodsVO> getGoodsListByIdArray(Integer[] ids) {
        List<TopicGoodsVO> goodsList = new ArrayList<>();
        for (Integer id : ids) {
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            criteria.andIsOnSaleEqualTo(true);
            criteria.andIdEqualTo(id);
            Goods goods = goodsMapper.selectByPrimaryKey(id);
            TopicGoodsVO goodsVO = new TopicGoodsVO();
            BeanUtils.copyProperties(goods, goodsVO);
            goodsList.add(goodsVO);
        }
        return goodsList;
    }

    /**
     * @description: 根据id获得Topic
     * @param: id
     * @return: Topic
     * @Author: liusenbiao
     */
    private Topic getTopicById(Integer id) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExampleWithBLOBs(topicExample);
        return topics.isEmpty() ? null : topics.get(0);
    }
}